package vincent.com.layouttest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import vincent.com.layouttest.R;

/**
 * Created by chenzhengwen on 2015/5/19.
 */
public class ColorStatusBarLayout extends RelativeLayout {
//    private Rect mRect;

    private int mColor;

    private Drawable mColorDrawable;

    private int mStatusBarHeight;

    public ColorStatusBarLayout(Context context) {
        this(context, null);
    }

    public ColorStatusBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorStatusBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedValue tv = new TypedValue();
        getContext().getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
        mColor = tv.data;

        mColorDrawable = new ColorDrawable(mColor);

        mStatusBarHeight = getStatusBarHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int mWidth = getWidth();
        Rect rect = new Rect(0, 0, mWidth, mStatusBarHeight);

        Log.i("czw_colorstatusbar", "mWidth: " + mWidth + " | rect: " + rect.left + ", " + rect.top + ", "
                                + rect.right + ", " + rect.bottom + " | color: " + mColor);

        int count = canvas.save();
        mColorDrawable.setBounds(rect);
        mColorDrawable.draw(canvas);
        canvas.restoreToCount(count);
    }

    private int getStatusBarHeight(){
        int height = 0;
        int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        height = getResources().getDimensionPixelSize(resId);

        return height;
    }

}
